import {EventEmitter, Injectable} from '@angular/core';

@Injectable()
export class ProgressBarService {
  public updateProgressBar$: EventEmitter<any>;

  private requestRunning = 0;

  constructor() {
    this.updateProgressBar$ = new EventEmitter();
  }

  public list(): number {
    return this.requestRunning;
  }

  public increase(): void {
    this.requestRunning++;
    if (this.requestRunning === 1) {
      this.updateProgressBar$.emit('indeterminate');
    }
  }

  public decrease(): void  {
    if (this.requestRunning > 0) {
      this.requestRunning--;
      if (this.requestRunning === 0) {
        this.updateProgressBar$.emit('none');
      }
    }
  }
}
